package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.Buyer;
import com.pro.springbootshop.entity.Seller;
import com.pro.springbootshop.entityExtend.BuyerReturn;
import com.pro.springbootshop.entityExtend.SellerReturn;
import com.pro.springbootshop.mapper.SellerMapper;
import com.pro.springbootshop.service.SellerService;
import com.pro.springbootshop.util.SellerTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SellerServiceImpl implements SellerService {
    public SellerMapper sellerMapper;
    @Autowired
    public void setUserMapper (SellerMapper sellerMapper) {
        this.sellerMapper = sellerMapper;
    }
    @Autowired
    SellerTokenUtil sellerTokenUtil;
    @Override
    public String loginSeller(Seller seller) {
        Seller newSeller=sellerMapper.sellerLogin(seller.getSellerId(),seller.getSellerPw());
        if(newSeller==null){
            return null;
        }else{
            return sellerTokenUtil.getToken(seller);
        }
    }

    @Override
    public Seller getSeller(String token) {
        Map<String,String> sellerMap=sellerTokenUtil.parseToken(token);
        String sellerId=sellerMap.get("sellerId");
        Seller seller=sellerMapper.sellerSearch(sellerId);
        if(seller==null){
            return null;
        }else {
            seller.setSellerPw(null);
            return seller;
        }
    }

    @Override
    public String updateSeller(Seller seller) {
        Seller newSeller=sellerMapper.sellerSearch(seller.getSellerId());
        seller.setSellerPw(newSeller.getSellerPw());
        seller.setId(newSeller.getId());
        int i=sellerMapper.updateById(seller);
        if(i>0){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    @Override
    public String updateSellerPw(String oldSellerPw, String sellerPw, String token) {
        Map<String,String> sellerMap=sellerTokenUtil.parseToken(token);
        String sellerId=sellerMap.get("sellerId");
        Seller seller=sellerMapper.sellerLogin(sellerId,oldSellerPw);
        if(seller!=null){
            sellerMapper.updatePassword(sellerPw,sellerId);
            return  "修改成功";
        }else {
            return "密码错误";
        }
    }

    @Override
    public String insertSeller(Seller seller) {
        if(sellerMapper.sellerSearch(seller.getSellerId())==null){
            int i=sellerMapper.insert(seller);
            if(i>0){
                return "注册成功";
            }else {
                return "注册失败";
            }
        }else {
            return "账号已存在";
        }
    }

    @Override
    public SellerReturn pageGetSeller(Integer page, Integer limit) {
        int pageNo=(page-1)*limit;
        int total=sellerMapper.selectList(null).size();
        List<Seller> sellerList=sellerMapper.adminGetSeller(pageNo,limit);
        return new SellerReturn(sellerList,total);
    }

    @Override
    public String adminDeleteSeller(String sellerId) {
        int i=sellerMapper.adminDeleteSeller(sellerId);
        if(i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}
